// timer0 overflw
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

#include <util/delay.h>
#include "timer.h"
#include "common.h"
volatile unsigned char muxCount=0; // multiplexer counter for selecting the digit on seven seg LED display
volatile int reading=12345;
unsigned  char SEVEN_SEG[] = {
											0x3F,
											0x06,
											0x5B,
											0x4F,
											0x66,
											0x6D,
											0x7D,
											0x07,
											0x7F,
											0x6F,
											0x77,
											0x7C,
											0x39,
											0x5E,
											0x79,
											0x71
										};
										
ISR(TIMER0_COMP_vect) {
	unsigned char digit;
    // process the timer1 overflow here
	if(muxCount>4)
		muxCount=0;
	
	digit = (reading /  x_to_the_n(10,muxCount))%10;
	
	SEVEN_SEGMENT_PORT = SEVEN_SEG[digit];
	
	MUX_PORT =(MUX_PORT & 0b111) /*keep PORTA0-2 untouched*/ | _BV(muxCount+3); // PORTA 3-7 used for MUX
	
	muxCount++;
}
int x_to_the_n (int x,int n);
int x_to_the_n (int x,int n)
{
    int i; /* Variable used in loop counter */
    int number = 1;

    for (i = 0; i < n; ++i)
        number *= x;

    return(number);
}